package com.yueyang.sojourn.server.utils;

/**
 * 证件验证工具
 * @author Andy.yang
 * @version v1
 * @summary
 * @Copyright (c) 2019, YueYang Group All Rights Reserved.
 * @since 2020/3/19
 */
public class CardValidUtil {
    /**手机号**/
    public static final String REGEX_MOBILE_EXACT = "^((13[0-9])|(14[5,7])|(15[0-3,5-9])|(16[6])|(17[0,1,3,5-8])|(18[0-9])|(19[8,9]))\\d{8}$";

    /**电话**/
    public static final String REGEX_TEL = "^0\\d{2,3}[- ]?\\d{7,8}";

    /**身份证15位**/
    public static final String REGEX_ID_CARD15 = "^[1-9]\\d{7}((0\\d)|(1[0-2]))(([0|1|2]\\d)|3[0-1])\\d{3}$";

    /**身份证18位**/
    public static final String REGEX_ID_CARD18 = "^[1-9]\\d{5}[1-9]\\d{3}((0\\d)|(1[0-2]))(([0|1|2]\\d)|3[0-1])\\d{3}([0-9Xx])$";

    /**回乡证**/
    public static final String REGEX_RETURN_HOME = "^(H|M)(\\d{10})$";

    /**台胞证**/
    public static final String REGEX_TAIWAN_CN = "(^\\d{8})$";
    /**港澳通行证**/
    public static final String REGEX_HK_PASS = "^([A-Z]\\d{6,10}(\\w1)?)$";
    /**台湾通行证**/
    public static final String REGEX_TW_PASS = "(^[a-zA-Z][0-9]{9})$";

    /**护照**/
    public static final String REGEX_PASSPORT = "^1[45][0-9]{7}|([P|p|S|s]\\d{7})|([S|s|G|g]\\d{8})|([Gg|Tt|Ss|Ll|Qq|Dd|Aa|Ff]\\d{8})|([H|h|M|m]\\d{8,10})$";

    /**
     * 验证身份证号码
     * @param idCard
     * @return
     */
    public static boolean validIdCard(String idCard){
        if(idCard == null || idCard.trim().isEmpty()){
            return false;
        }
        idCard = idCard.trim();
        if(!idCard.matches(REGEX_ID_CARD15) && !idCard.matches(REGEX_ID_CARD18)){
            return false;
        }
        if (idCard.length() == 18) {
            try {
                char[] charArray = idCard.toCharArray();
                //前十七位加权因子
                int[] idCardWi = {7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2};
                //这是除以11后，可能产生的11位余数对应的验证码
                String[] idCardY = {"1", "0", "X", "9", "8", "7", "6", "5", "4", "3", "2"};
                int sum = 0;
                for (int i = 0; i < idCardWi.length; i++) {
                    int current = Integer.parseInt(String.valueOf(charArray[i]));
                    int count = current * idCardWi[i];
                    sum += count;
                }
                char idCardLast = charArray[17];
                int idCardMod = sum % 11;
                if (idCardY[idCardMod].toUpperCase().equals(String.valueOf(idCardLast).toUpperCase())) {
                    return true;
                } else {
                    return false;
                }

            } catch (Exception e) {
                e.printStackTrace();
                return false;
            }
        }
        return true;
    }


    /**
     * 验证护照
     * @param passport
     * @return
     */
    public static boolean validPassport(String passport) {
        return validCommon(passport,REGEX_PASSPORT);
    }

    /**
     * 验证港澳通行证
     * @param hkPass
     * @return
     */
    public static boolean validHKPass(String hkPass) {
        return validCommon(hkPass,REGEX_HK_PASS);
    }

    /**
     * 验证台湾通行证
     * @param twPass
     * @return
     */
    public static boolean validTWPass(String twPass) {
        return validCommon(twPass,REGEX_TW_PASS);
    }

    /**
     * 验证台胞证
     * @param twCN
     * @return
     */
    public static boolean validTaiwanCN(String twCN) {
        return validCommon(twCN,REGEX_TAIWAN_CN);
    }

    /**
     * 验证回乡证
     * @param returnHome
     * @return
     */
    public static boolean validReturnHome(String returnHome) {
        return validCommon(returnHome,REGEX_RETURN_HOME);
    }


    /**
     * 通用验证
     * @param card
     * @return
     */
    private static boolean validCommon(String card,String rex) {
        if (card == null || card.trim().isEmpty()) {
            return false;
        }
        card = card.trim();
        return card.matches(rex);
    }

    public static boolean validMobile(String mobile){
        return validCommon(mobile,REGEX_MOBILE_EXACT);
    }

    public static void main(String args[]){
        String idCard = "xxx";
        boolean rs = validIdCard(idCard);
        System.out.println(rs);
    }
}

